<?php

class Users extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('user');
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
        } else {redirect('user_login/user', 'refresh');}

        $this->load->model('user');
        $this->load->model('user_role');
        $this->load->library('user_id');
        $this->load->helper('user_helper');

        if ($this->user_id->cek() == 0) {
            redirect('user_login/user', 'refresh');
        }
    }

    function index() {
        $data['id'] = $this->user_id->id();
        
        $rows = $this->user->index();
        $data['content'] = 'users/index';
        $this->load->view('template/user', $data);
    }

    function register() {
        $data['id'] = $this->user_id->id();
        
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data['content'] = 'login_user/register';
        $this->load->view('template/user', $data);
    }

    function edit() {
        $data['id'] = $this->user_id->id();
        
        $user_id = $this->uri->segment(3);
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data['drop'] = $this->user_role->get_array();
        $data['users'] = $this->user->edit($user_id);
        $data['content'] = 'users/edit';
        $this->load->view('template/user', $data);
    }

    function create() {
        $this->load->library('form_validation');
        $name = $this->form_validation->set_rules('name', 'Name', 'required');
        $username = $this->form_validation->set_rules('username', 'Usernmae', 'required|min_length[5]|max_length[12]|is_unique[users.username]');
        $email = $this->form_validation->set_rules('email', 'email', 'required|valid_email|is_unique[users.email]');
        $password = $this->form_validation->set_rules('password', 'Password', 'required|min_length[5]|max_length[12]|required|matches[verify_password]');
        $verify_password = $this->form_validation->set_rules('verify_password', 'Verify password', 'required');

        if ($this->form_validation->run() == FALSE) {
            $data['content'] = 'login/register';
            $this->load->view('template/costumer', $data);
        } else {
            $date = date('Y-m-d');
            $name = $this->input->post('name', TRUE);
            $username = $this->input->post('username', TRUE);
            $email = $this->input->post('email', TRUE);
            $password = $this->input->post('password', TRUE);
            $password = md5($password);
            $last_name = $this->input->post('last_name', TRUE);
            $user_level = $this->input->post('user_level', TRUE);
            $this->user->create($name, $username, $email, $password, $last_name, $user_level, $date);
        }
    }

    function update() {
        $this->load->library('form_validation');
        $name = $this->form_validation->set_rules('name', 'Name', 'required');
        $username = $this->form_validation->set_rules('username', 'Usernmae', 'required|min_length[5]|max_length[12]|is_unique[users.username]');
        $email = $this->form_validation->set_rules('email', 'email', 'required|valid_email|is_unique[users.email]');

        if ($this->input->post('password', TRUE)) {
            $password = $this->form_validation->set_rules('password', 'Password', 'required|min_length[5]|max_length[12]|required|matches[verify_password]');
            $verify_password = $this->form_validation->set_rules('verify_password', 'Verify password', 'required');
        }

        if ($this->form_validation->run() == FALSE) {
            $data['content'] = 'login/register';
            $this->load->view('template/costumer', $data);
        } else {
            $id = $this->input->post('id', TRUE);
            $name = $this->input->post('name', TRUE);
            $username = $this->input->post('username', TRUE);
            $email = $this->input->post('email', TRUE);
            $last_name = $this->input->post('last_name', TRUE);
            $user_level = $this->input->post('user_level', TRUE);
            if ($this->input->post('password', TRUE)) {
                $password = $this->input->post('password', TRUE);
                $password = md5($password);
            } else {
                $password = '';
            }
            $this->user->update($id, $name, $username, $email, $password, $last_name, $user_level);
        }
    }
    
    function delete(){
        $id = $this->uri->segment(3);
        $this->user->delete($id);
    }

    function logout() {
        $this->session->unset_userdata('logged_in');
        redirect('user_login/user', 'refresh');
    }
    
    

}

?>
